package com.wanbatv.wangwangba;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.wanbatv.wangwangba.util.Behavior;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.security.MessageDigest;
import java.util.Timer;
import java.util.TimerTask;


public class ShowBofangActivity extends Activity implements MediaPlayer.OnPreparedListener,MediaPlayer.OnCompletionListener,SurfaceHolder.Callback,SeekBar.OnSeekBarChangeListener {
    private SurfaceView surfaceView;
    MediaPlayer mediaPlay;
    SurfaceHolder surfaceholder;

    private ImageView wws_daiji_iv;
    private ImageView wws_zhezhao;
    private SeekBar seekbar;
    private TextView wws_tv1;
    private TextView wws_tv2;
    private ImageView wws_bofang_iv;
    private ImageView wws_houtui_iv;
    private ImageView wws_qianjin_iv;
    private ImageView wws_dianzan_iv;
    private ImageView wws_zanting_iv;
    private ImageView wws_kuaijin_iv;
    private ImageView wws_kuaitui_iv;
    private boolean ISCHANGE=false;
    private boolean IFZAN=false;
    private boolean IFGONE=true;

    private static final int MSG_PLAY_PROGRESS = 1;
    private static final int PLAY_MESSAGE=2;
    private static final int MSG_UP_DOWN=3;

    String url_qian="http://121.201.7.173:8087/api/wangwangba/show/";
    String dianzan="http://121.201.7.173:8087/api/wangwangba/like/";
    String qixiao_dianzan="http://121.201.7.173:8087/api/wangwangba/unlike/";
    String TOKNE=null;
    int content_code;
    String content_name=null;
    //标记快进快退按键状态：正常，未进行快进快退操作
    private static final int STATE_FAST_SEEK_IDLE = 0;
    //标记快进快退按键状态：用户长按着按键，但还未开始快进快退
    private static final int STATE_FAST_SEEK_PREPARED = 1;
    //标记快进快退按键状态：已经开始快进快退
    private static final int STATE_FAST_SEEK_START = 2;
    private int mStateFastSeek = STATE_FAST_SEEK_IDLE;
    private KeyEvent mLastEvent = null;
    private long KUAIJIN_TIME=0;
    private KeyEvent mDownEvent = null;

    Handler mHandler = new Handler(Looper.getMainLooper()) {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_PLAY_PROGRESS:
                    int time=mediaPlay.getCurrentPosition();
                    int a=time/1000;
                    int m=a/60;
                    int s=a%60;
                    if(s<10){
                        wws_tv1.setText(m+":0"+s);}
                    else{
                        wws_tv1.setText(m+":"+s);
                    }
                    if(ISCHANGE==false) {
                        seekbar.setProgress(mediaPlay.getCurrentPosition());
                    }
                    mHandler.removeMessages(MSG_PLAY_PROGRESS);
                    mHandler.sendEmptyMessageDelayed(MSG_PLAY_PROGRESS, 500);
                    break;
                case PLAY_MESSAGE:
                    ISCHANGE=false;
                    mHandler.removeMessages(PLAY_MESSAGE);
                    break;
                case MSG_UP_DOWN:
                    if(IFGONE==false && wws_zhezhao.getVisibility()==View.VISIBLE){

                        all_gone();
                    }else {
                        IFGONE=false;
                    }

                    mHandler.removeMessages(MSG_UP_DOWN);
                    mHandler.sendEmptyMessageDelayed(MSG_UP_DOWN, 3500);
                    break;
                case 10:
                    if(wws_zanting_iv.getVisibility()==View.VISIBLE){
                        wws_zanting_iv.setVisibility(View.GONE);
                    }
                    mHandler.removeMessages(10);
                    mHandler.sendEmptyMessageDelayed(10, 1500);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_bofang);
        surfaceView=(SurfaceView)findViewById(R.id.wws_surface);
        surfaceholder = surfaceView.getHolder();
        surfaceholder.setFixedSize(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        surfaceholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        surfaceholder.addCallback(this);
        wws_daiji_iv=(ImageView)findViewById(R.id.wws_daiji_iv);
        wws_zhezhao=(ImageView)findViewById(R.id.wws_zhezhao);
        seekbar=(SeekBar)findViewById(R.id.seekbar);
        wws_tv1=(TextView)findViewById(R.id.wws_tv1);
        wws_tv2=(TextView)findViewById(R.id.wws_tv2);
//        wws_bofang_iv=(ImageView)findViewById(R.id.wws_bofang_iv);
//        wws_houtui_iv=(ImageView)findViewById(R.id.wws_houtui_iv);
//        wws_qianjin_iv=(ImageView)findViewById(R.id.wws_qianjin_iv);
//        wws_dianzan_iv=(ImageView)findViewById(R.id.wws_dianzan_iv);
        wws_zanting_iv=(ImageView)findViewById(R.id.wws_zanting_iv);
        wws_kuaijin_iv=(ImageView)findViewById(R.id.wws_kuaijin_iv);
        wws_kuaitui_iv=(ImageView)findViewById(R.id.wws_kuaitui_iv);
        seekbar.setOnSeekBarChangeListener(this);

        if(Login2Activity.url_wws17==true){
            wws_daiji_iv.setVisibility(View.GONE);
        }
        if(Login2Activity.url_wws18==true){
            wws_daiji_iv.setVisibility(View.GONE);
        }
//
//        wws_houtui_iv.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                IFGONE = true;
//                mediaPlay.seekTo(mediaPlay.getCurrentPosition() - 5000);
//            }
//        });
//
//        wws_qianjin_iv.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                IFGONE=true;
//                mediaPlay.seekTo(mediaPlay.getCurrentPosition() + 5000);
//            }
//        });
//        wws_bofang_iv.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                IFGONE=true;
//                if(mediaPlay.isPlaying()) {
//                    mediaPlay.pause();
//                    wws_zanting_iv.setVisibility(View.VISIBLE);
//                }else{
//                    mediaPlay.start();
//                    wws_zanting_iv.setVisibility(View.GONE);
//                }
//
//            }
//        });
//        wws_dianzan_iv.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (IFZAN) {
//                    Behavior.onEvent(ShowBofangActivity.this, 3030006, "取消点赞");
//                    wws_dianzan_iv.setImageResource(R.drawable.guangbiao_src_dianzanqian);
//                    IFZAN = false;
//                    RequestQueue rq = Volley.newRequestQueue(ShowBofangActivity.this.getApplicationContext());
//                    JsonObjectRequest request = new JsonObjectRequest(qixiao_dianzan+TOKNE, null,
//                            new Response.Listener<JSONObject>() {
//
//                                @Override
//                                public void onResponse(JSONObject jsonObject) {
//                                    Toast.makeText(ShowBofangActivity.this,"取消点赞",Toast.LENGTH_SHORT).show();
//                                }
//                            }, new Response.ErrorListener() {
//                        @Override
//                        public void onErrorResponse(VolleyError volleyError) {
//                        }
//                    }
//
//                    );
//                    rq.add(request);
//                } else {
//                    Behavior.onEvent(ShowBofangActivity.this, 3030005, "点赞");
//                    wws_dianzan_iv.setImageResource(R.drawable.guangbiao_src_dianzanhou);
//                    IFZAN = true;
//                    RequestQueue rq = Volley.newRequestQueue(ShowBofangActivity.this.getApplicationContext());
//                    JsonObjectRequest request = new JsonObjectRequest(dianzan+TOKNE, null,
//                            new Response.Listener<JSONObject>() {
//
//                                @Override
//                                public void onResponse(JSONObject jsonObject) {
//                                    Toast.makeText(ShowBofangActivity.this,"点赞成功",Toast.LENGTH_SHORT).show();
//                                }
//                            }, new Response.ErrorListener() {
//                        @Override
//                        public void onErrorResponse(VolleyError volleyError) {
//                        }
//                    }
//
//                    );
//                    rq.add(request);
//                }
//                IFGONE = true;
//            }
//        });
        mHandler.sendEmptyMessage(MSG_UP_DOWN);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_show_bofang, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mediaPlay=new MediaPlayer();
        mediaPlay.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlay.setDisplay(surfaceholder);
        mediaPlay.setOnCompletionListener(ShowBofangActivity.this);

        try {
            mediaPlay.setOnPreparedListener(ShowBofangActivity.this);
            if(Login2Activity.url_wws11==true){
                mediaPlay.setDataSource(url_qian+Login2Activity.shipin_url11+jiami(Login2Activity.dizhi11));
//                TOKNE=WangwangShowActivity.shipin_url1;
//                content_name=WangwangShowActivity.dizhi1;
//                content_code=WangwangShowActivity.content_code1;
            }
            else if(Login2Activity.url_wws12==true){
                mediaPlay.setDataSource(url_qian+Login2Activity.shipin_url12+jiami(Login2Activity.dizhi12));
//                TOKNE=WangwangShowActivity.shipin_url2;
//                content_name=WangwangShowActivity.dizhi2;
//                content_code=WangwangShowActivity.content_code2;
            }
            else if(Login2Activity.url_wws13==true){
                mediaPlay.setDataSource(url_qian+Login2Activity.shipin_url13+jiami(Login2Activity.dizhi13));
//                TOKNE=WangwangShowActivity.shipin_url3;
//                content_name=WangwangShowActivity.dizhi3;
//                content_code=WangwangShowActivity.content_code3;
            }
            else if(Login2Activity.url_wws14==true){
                mediaPlay.setDataSource(url_qian+Login2Activity.shipin_url14+jiami(Login2Activity.dizhi14));
//                TOKNE=WangwangShowActivity.shipin_url4;
//                content_name=WangwangShowActivity.dizhi4;
//                content_code=WangwangShowActivity.content_code4;
            }
            else if(Login2Activity.url_wws15==true){
                mediaPlay.setDataSource(url_qian+Login2Activity.shipin_url15+jiami(Login2Activity.dizhi15));
//                TOKNE=WangwangShowActivity.shipin_url5;
//                content_name=WangwangShowActivity.dizhi5;
//                content_code=WangwangShowActivity.content_code5;
            }
            else if(Login2Activity.url_wws16==true){
                mediaPlay.setDataSource(url_qian+Login2Activity.shipin_url16+jiami(Login2Activity.dizhi16));
//                TOKNE=WangwangShowActivity.shipin_url6;
//                content_name=WangwangShowActivity.dizhi6;
//                content_code=WangwangShowActivity.content_code6;
            }
            else if(Login2Activity.url_wws17==true){
                mediaPlay.setDataSource(url_qian+Login2Activity.shipin_url17+jiami(Login2Activity.dizhi17));
//                TOKNE=WangwangShowActivity.shipin_url6;
//                content_name=WangwangShowActivity.dizhi6;
//                content_code=WangwangShowActivity.content_code6;
            }
            else if(Login2Activity.url_wws18==true){
                mediaPlay.setDataSource(url_qian+Login2Activity.shipin_url18+jiami(Login2Activity.dizhi18));
//                TOKNE=WangwangShowActivity.shipin_url6;
//                content_name=WangwangShowActivity.dizhi6;
//                content_code=WangwangShowActivity.content_code6;
            }
            else{
                mediaPlay.setDataSource("http://www.wanbatv.com/dogtv/welcome.TS");
            }

            mediaPlay.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }


    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }


    @Override
    public void onCompletion(MediaPlayer mp) {
        mHandler.removeMessages(MSG_PLAY_PROGRESS);
        mediaPlay.release();
        finish();
    }


    @Override
    public void onPrepared(MediaPlayer mp) {
    mp.start();
//        if(WangwangShowActivity.IS_ZUO){
//        Behavior.onPlayContentStart(ShowBofangActivity.this,201,"全部分享",content_code,content_name);}
//        else{
//            Behavior.onPlayContentStart(ShowBofangActivity.this,202,"个人分享",content_code,content_name);
//        }
        wws_daiji_iv.setVisibility(View.GONE);
        int time=mediaPlay.getDuration();
        int a=time/1000;
        int m=a/60;
        int s=a%60;
        if(s<10){
        wws_tv2.setText(m+":0"+s);}
        else{
            wws_tv2.setText(m+":"+s);
        }
        seekbar.setMax(mediaPlay.getDuration());
        mHandler.sendEmptyMessage(MSG_PLAY_PROGRESS);

    }


    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if(fromUser){
            ISCHANGE=true;
            mediaPlay.seekTo(progress);
            if(mHandler.hasMessages(PLAY_MESSAGE)){
                mHandler.removeMessages(PLAY_MESSAGE);

            }
            mHandler.sendEmptyMessageDelayed(PLAY_MESSAGE,100);
        }


    }


    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }


    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {


    }

    @Override
    protected void onStop() {
        super.onStop();
//        if(mediaPlay.isPlaying()) {
//            mediaPlay.stop();
//        }
        mediaPlay.release();
        mHandler.removeMessages(MSG_PLAY_PROGRESS);
//        if(WangwangShowActivity.IS_ZUO){
//            Behavior.onPlayContentStop(ShowBofangActivity.this, 201, "全部分享", content_code, content_name);}
//        else{
//            Behavior.onPlayContentStop(ShowBofangActivity.this,202,"个人分享",content_code,content_name);
//        }
    }


    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if((keyCode == KeyEvent.KEYCODE_DPAD_LEFT)) {
            if (mStateFastSeek == STATE_FAST_SEEK_START) {
                mStateFastSeek = STATE_FAST_SEEK_IDLE;
                mLastEvent = null;
                mDownEvent =null;
                wws_kuaitui_iv.setVisibility(View.GONE);
            }
        }
        if((keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)) {
            if (mStateFastSeek == STATE_FAST_SEEK_START) {
                mStateFastSeek = STATE_FAST_SEEK_IDLE;
                mLastEvent = null;
                mDownEvent =null;
                wws_kuaijin_iv.setVisibility(View.GONE);
            }
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode!=KeyEvent.KEYCODE_BACK && wws_zhezhao.getVisibility()==View.INVISIBLE&&keyCode!=KeyEvent.KEYCODE_ENTER&&keyCode!=KeyEvent.KEYCODE_DPAD_CENTER){
            all_visibility();
            IFGONE=true;
        }
        else if(keyCode==KeyEvent.KEYCODE_ENTER){
            if(mediaPlay.isPlaying()){
                wws_zanting_iv.setVisibility(View.VISIBLE);
                mediaPlay.pause();
                IFGONE=true;
            }else {
                wws_zanting_iv.setVisibility(View.GONE);
                mediaPlay.start();
                IFGONE=true;
            }
        }
        else if(keyCode==KeyEvent.KEYCODE_DPAD_CENTER){
            if(mediaPlay.isPlaying()){
                wws_zanting_iv.setVisibility(View.VISIBLE);
                mediaPlay.pause();
                IFGONE=true;
            }else {
                wws_zanting_iv.setVisibility(View.GONE);
                mediaPlay.start();
                IFGONE=true;
            }
        }

        else if((keyCode == KeyEvent.KEYCODE_DPAD_LEFT)){
            if((seekbar.getVisibility()==View.INVISIBLE)){

                return true;
            }else {
                if (mStateFastSeek == STATE_FAST_SEEK_IDLE) {

                    mStateFastSeek = STATE_FAST_SEEK_PREPARED;
                    mDownEvent = event;
                } else if (mStateFastSeek == STATE_FAST_SEEK_PREPARED) {

                    final long deltaEventTime = event.getEventTime() - mDownEvent.getDownTime();
                    if (deltaEventTime > 300) {
                        mStateFastSeek = STATE_FAST_SEEK_START;
                        mLastEvent = event;
                        Log.d("KeyEvent", "@@@ onKeyDown at " + event.getEventTime());
                        if(mediaPlay.isPlaying()) {
                            mediaPlay.seekTo(mediaPlay.getCurrentPosition() - 2000);
                            wws_kuaitui_iv.setVisibility(View.VISIBLE);
                            IFGONE=true;
                        }else{
                            KUAIJIN_TIME-=2000;
                        }
                    }
                } else if (mStateFastSeek == STATE_FAST_SEEK_START) {
                    final long deltaEventTime = event.getEventTime() - mLastEvent.getEventTime();
                    if (deltaEventTime > 100) {
                        mLastEvent = event;
                        Log.d("KeyEvent", "@@@ onKeyDown at " + event.getEventTime());
                        if(mediaPlay.isPlaying()) {
                            mediaPlay.seekTo(mediaPlay.getCurrentPosition() - 2000);
                            wws_kuaitui_iv.setVisibility(View.VISIBLE);
                            IFGONE=true;
                        }else{
                            KUAIJIN_TIME-=2000;
                        }

                    }
                }

                return super.onKeyDown(keyCode, event);
            }
        }
        else if((keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)){
            Log.d("KeyEvent", "@@@ onKeyDown at 22");
            if((seekbar.getVisibility()==View.INVISIBLE)){

                return true;
            }else {
                if (mStateFastSeek == STATE_FAST_SEEK_IDLE) {
                    mStateFastSeek = STATE_FAST_SEEK_PREPARED;
                    mDownEvent = event;
                    Log.d("KeyEvent", "@@@ onKeyDown at 22 9");
                } else if (mStateFastSeek == STATE_FAST_SEEK_PREPARED) {
                    Log.d("KeyEvent", "@@@ onKeyDown at 22 10");
                    final long deltaEventTime = event.getEventTime() - mDownEvent.getDownTime();
                    Log.d("KeyEvent", "@@@ onKeyDown at"+deltaEventTime);
                    if (deltaEventTime > 300) {
                        mStateFastSeek = STATE_FAST_SEEK_START;
                        mLastEvent = event;
                        Log.d("KeyEvent", "@@@ onKeyDown at " + event.getEventTime());
                        if(mediaPlay.isPlaying()) {
                            mediaPlay.seekTo(mediaPlay.getCurrentPosition() + 2000);
                            wws_kuaijin_iv.setVisibility(View.VISIBLE);
                            IFGONE=true;
                            Log.d("KeyEvent", "@@@ onKeyDown at 22 11");
                        }else{
                            KUAIJIN_TIME+=2000;
                        }

                    }
                } else if (mStateFastSeek == STATE_FAST_SEEK_START) {
                    final long deltaEventTime = event.getEventTime() - mLastEvent.getEventTime();
                    if (deltaEventTime > 100) {
                        mLastEvent = event;
                        Log.d("KeyEvent", "@@@ onKeyDown at " + event.getEventTime());
                        if(mediaPlay.isPlaying()) {
                            mediaPlay.seekTo(mediaPlay.getCurrentPosition() + 2000);
                            wws_kuaijin_iv.setVisibility(View.VISIBLE);
                            IFGONE=true;
                        }else{
                            KUAIJIN_TIME+=2000;
                        }

                    }
                }
                return super.onKeyDown(keyCode, event);
            }
        }
        IFGONE=true;
        Log.d("KeyEvent", "@@@"+event);
        return super.onKeyDown(keyCode, event);
    }

    public void all_gone(){
        Animation translateAnimation = new TranslateAnimation( 0,0,0,1000);
        translateAnimation.setDuration(500);
        seekbar.startAnimation(translateAnimation);
        wws_tv1.startAnimation(translateAnimation);
        wws_tv2.startAnimation(translateAnimation);
//        wws_qianjin_iv.startAnimation(translateAnimation);
//        wws_bofang_iv.startAnimation(translateAnimation);
//        wws_houtui_iv.startAnimation(translateAnimation);
        wws_zhezhao.startAnimation(translateAnimation);
        seekbar.setVisibility(View.INVISIBLE);
        wws_tv1.setVisibility(View.INVISIBLE);
        wws_tv2.setVisibility(View.INVISIBLE);
//        wws_qianjin_iv.setVisibility(View.INVISIBLE);
//        wws_bofang_iv.setVisibility(View.INVISIBLE);
//        wws_houtui_iv.setVisibility(View.INVISIBLE);
        wws_zhezhao.setVisibility(View.INVISIBLE);
    }
    public void all_visibility(){
        Animation translateAnimation = new TranslateAnimation( 0,0,1000,0);
        translateAnimation.setDuration(500);
        seekbar.startAnimation(translateAnimation);
        wws_tv1.startAnimation(translateAnimation);
        wws_tv2.startAnimation(translateAnimation);
//        wws_qianjin_iv.startAnimation(translateAnimation);
//        wws_bofang_iv.startAnimation(translateAnimation);
//        wws_houtui_iv.startAnimation(translateAnimation);
        wws_zhezhao.startAnimation(translateAnimation);
        seekbar.setVisibility(View.VISIBLE);
        wws_tv1.setVisibility(View.VISIBLE);
        wws_tv2.setVisibility(View.VISIBLE);
//        wws_qianjin_iv.setVisibility(View.VISIBLE);
//        wws_bofang_iv.setVisibility(View.VISIBLE);
//        wws_houtui_iv.setVisibility(View.VISIBLE);
        wws_zhezhao.setVisibility(View.VISIBLE);
    }

    public static String url_bofang=null;
    private String jiami(String video_url){
        String url_team2=video_url;

        long time=System.currentTimeMillis();
        final String shijianchuo=Long.toHexString(time);
        final String key="w2nBa7VS1";

        String url_key=key+url_team2+shijianchuo;
        String key_1=string2MD5(url_key);
        String url_team="?auth_token="+key_1+"&auth_time="+shijianchuo;
        return url_team;
    }
    public static String string2MD5(String inStr){
        MessageDigest md5 = null;
        try{
            md5 = MessageDigest.getInstance("MD5");
        }catch (Exception e){
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }
        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];

        for (int i = 0; i < charArray.length; i++)
            byteArray[i] = (byte) charArray[i];
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++){
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16)
                hexValue.append("0");
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString().toLowerCase();

    }
    @Override
    protected void onResume() {
        super.onResume();
        Behavior.onPageResume(this,300001,"进入播放页面");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Behavior.onPagePause(this,300001, "退出播放页面");
    }
}
